package com.boarsoft.hibernate;

import java.util.Stack;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateSessionTx {
	public static final ThreadLocal<HibernateSessionTx> holder = new ThreadLocal<HibernateSessionTx>();

	protected Session session;
	protected Stack<Transaction> txStack = new Stack<Transaction>();

	public static HibernateSessionTx get() {
		return holder.get();
	}

	public static void set(HibernateSessionTx st) {
		holder.set(st);
	}

	public static void remove() {
		holder.remove();
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public boolean hasTransaction() {
		return !txStack.isEmpty();
	}

	public Transaction peekTransaction() {
		if (txStack.isEmpty()) {
			return null;
		}
		return txStack.peek();
	}

	public Transaction popTransaction() {
		if (txStack.isEmpty()) {
			return null;
		}
		return txStack.pop();
	}

	public void pushTransaction(Transaction tx) {
		txStack.push(tx);
	}
}
